<?php
/*
 * File: AppServiceProvider.php
 * Created Time: 2024-05-29 11:27:52 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-29 5:25:22 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2024 成都睿創科技有限公司
 */



namespace App\Providers;

use App\Models\ShopStripe;
use Laravel\Cashier\Cashier;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use App\Http\Controllers\Common\MonologsController  as Logs;//日志类

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() {
        // Stripe支付設置 
        $ShopStripe = new ShopStripe();
        $info = $ShopStripe->where('stripe_status', 1)->first();
        if(!empty($info)){
            config(['cashier.key' => $info['stripe_key']]);
            config(['cashier.secret' => $info['stripe_secret']]);
        }else{
            $stripe_key='pk_test_51PYfDhExgXF0QlkuL5suxTSse4SNmst9Kcd3y44HxJ1X4xcvcJ1pzdInX5RlDrCSdLIsoseBvu3qALJ6laDkDOWA00WWuLGK3J';
            $stripe_secret='sk_test_51PYfDhExgXF0Qlkuf66EjL7FBqlmCEfCsobhJZ9h3Rl3lIFww4OsGAPIDE92z3wxdcnlSEJ34mvsrKGfDGDqo6YY00InbSB0oh';
            config(['cashier.key' => $stripe_key]);
            config(['cashier.secret' => $stripe_secret]);
        }
        //打印sql语句到日志
        $logSql = env('SQL_QUERY_SAVE', 'false');
        if($logSql){
            DB::listen(function($query){
                //剔除命令行sql
                if(app()->runningInConsole()) return;
                $request = request();
                //防止重复录入形成死循环 sql_log有关的表不显示
                if(strpos($query->sql,'sql_log' !=false)) return;
                $sql = $query->sql;
                $bindings = $query->bindings;
                foreach ($bindings as $binding) {
                    $sql = preg_replace('/\?/', is_numeric($binding) ? $binding : "'$binding'", $sql, 1);
                }
                
                Logs::info('sql语句:'.$sql.';', ['查询完成'] ,'SqlQuery'.date('Y-m-d') ,false);
            });
        }
    }




    


    
}
